package com.chinaisafe.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;
import com.chinaisafe.common.sdk.constant.UserConstant;
import com.chinaisafe.gateway.client.auth.UaaAuthHelper;
import com.chinaisafe.user.entity.SysTenantApp;
import com.chinaisafe.user.mapper.SysTenantAppMapper;
import com.chinaisafe.user.service.SysTenantAppService;
import com.chinaisafe.user.vo.request.SysTenantAppReqVO;
import com.chinaisafe.user.vo.response.SysTenantAppRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Optional;
/**
 * SysTenantAppServiceImpl
 * Description：租户和应用服务实现类
 *
 * @author tby
 * @date 2024-07-12 10:04:27
 */
@Service
public class SysTenantAppServiceImpl extends ServiceImpl<SysTenantAppMapper, SysTenantApp> implements SysTenantAppService {

    @Override
    public IPage<SysTenantAppRespVO> getPageList(SysTenantAppReqVO sysTenantAppReqVO) {
        // 分页设置
        Page<SysTenantApp> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysTenantAppReqVO);
        sysTenantAppReqVO.setTenancyCode(UaaAuthHelper.getCurrentTenancyCode());
        return baseMapper.getPageList(page, sysTenantAppReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysTenantAppRespVO add(SysTenantAppReqVO sysTenantAppReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysTenantAppReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysTenantAppRespVO update(SysTenantAppReqVO sysTenantAppReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysTenantAppReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysTenantAppRespVO addOrUpdate(SysTenantAppReqVO sysTenantAppReqVO) {
        Long sysTenantAppId = sysTenantAppReqVO.getId();
        if (sysTenantAppId != null){
            // 编辑
            SysTenantApp sysTenantApp = baseMapper.selectById(sysTenantAppId);
            Optional.ofNullable(sysTenantApp).orElseThrow(() -> BizException.build("未找到要修改的租户和应用信息"));
        }
        SysTenantApp sysTenantApp2Save = sysTenantAppReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysTenantApp2Save);
        if (success) {
            return SysTenantAppRespVO.convertFromEntity(sysTenantApp2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysTenantAppRespVO getDetailById(Long sysTenantAppId) {
        SysTenantApp sysTenantApp = baseMapper.selectById(sysTenantAppId);
        Optional.ofNullable(sysTenantApp).orElseThrow(() -> BizException.build("未找到对应租户和应用信息"));
        return SysTenantAppRespVO.convertFromEntity(sysTenantApp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysTenantAppIds) {
        if (sysTenantAppIds.length > 0) {
            this.removeByIds(Arrays.asList(sysTenantAppIds));
        }
    }




}
